package exception;

public class TryCatchDemo {
    public static void main(String[] args) {
        System.out.println("main方法开始");
        try {
            /*String str=null;
            System.out.println(str.length());*///NullPointerException
            String str2="abc";
            System.out.println(str2.charAt(5));//StringIndexOutOfBoundsException


            System.out.println("!!!!!!");
            //当发生异常时，异常下面的代码不会执行
            //在try catch块中，可以捕获多个异常，多个异常用|隔开
        }/*catch (NullPointerException e){
            System.out.println("空指针异常");
        }catch (StringIndexOutOfBoundsException e){
            System.out.println("字符串索引越界异常");
        }*/catch (NullPointerException | StringIndexOutOfBoundsException e){
            System.out.println("空指针异常或字符串索引越界异常");
        }catch (Exception e){
            System.out.println("未知异常"+e);
        }

        System.out.println("main方法结束");


    }
}
